package com.example.demo.concurrent.CountLatch;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;

/**
 * CountDownLatch 练习
 */

@Slf4j(topic = "c.TestCountLatch")
public class TestCountLatch {

    /**
     * CountDownLatch 练习 -->倒计时锁
     * @param args
     */
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(3);
        new Thread(()->{
            log.debug("running...");
            sleeper(1000);
            latch.countDown();
            log.debug("end...");
        },"t1").start();
        new Thread(()->{
            log.debug("running...");
            sleeper(2000);
            latch.countDown();
            log.debug("end...");
        },"t2").start();
        new Thread(()->{
            log.debug("running...");
            sleeper(1500);
            latch.countDown();
            log.debug("end...");
        },"t3").start();

        log.debug("awaiting...");
        latch.await();
        log.debug("awaiting end...");
    }

    public static void sleeper(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}